package me.zhengjie.business.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.aop.service.BisLogService;
import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.*;
import me.zhengjie.business.service.dto.ZhsqQueryCriteria;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lyl
 * @since 2023/10/13
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "居家照护申请管理")
@RequestMapping("/api/zhsqHome")
public class ZhsqHomeController {

    private final ZhsqService zhsqService;

    private final ZhsqHomeService zhsqHomeService;


    private final GsglService gsglService;

    @GetMapping
    @ApiOperation("查询机构申请中的照护")
    @PreAuthorize("@el.check('zhsq:list')")
    public ResponseEntity<Object> queryZhsq(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        criteria.setBisZhsqZhlb("03");

        List<String> list = new ArrayList<>();
        list.add("00");
        list.add("01");
        list.add("02");
        list.add("03");
        list.add("04");
        list.add("54");
        list.add("55");
        criteria.setBisZhsqZt(list);

        return new ResponseEntity<>(zhsqHomeService.queryAll(criteria, pageable), HttpStatus.OK);
    }


    @PostMapping
    @ApiOperation("PC端新增居家照护申请")
    @PreAuthorize("@el.check('zhsq:add')")
    public ResponseEntity<Object> createZhsqHome(@Validated @RequestBody Zhsq resources, @RequestParam String ids, HttpServletRequest request) {
        System.out.println("=================请求数据" + resources);
        return new ResponseEntity<>(PageUtil.toMpContent(zhsqHomeService.createZhsqHome(resources, ids)), HttpStatus.OK);
    }


    @PutMapping
    @Log("PC端修改居家照护申请")
    @ApiOperation("PC端修改居家照护申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> updateZhsqHome(@Validated @RequestBody Zhsq resources, @RequestParam String ids, HttpServletRequest request) {

        return new ResponseEntity<>(PageUtil.toMpContent(zhsqHomeService.updateZhsq(resources,ids)), HttpStatus.OK);
    }

    @PutMapping(value = "/submitZhsqHome")
    @Log("机构提交照护申请")
    @ApiOperation("机构提交照护申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> submitZhsqHome(@Validated @RequestBody Zhsq resources, @RequestParam String ids){
        return new ResponseEntity<>(zhsqHomeService.submitZhsqHome(resources, ids), HttpStatus.OK);
    }
}
